<?php
$objXajax->registerFunction('frmuser_loadGrid');
$objXajax->registerFunction('frmuser_save');
$objXajax->registerFunction('frmuser_init');
$objXajax->registerFunction('frmuser_changePass');
$objXajax->registerFunction('frmuser_edit');
function frmuser_init(){ 
    global $objDbSelect,$lang,$userid,$powerid,$powertypeid;
    $objResponse = new xajaxResponse();
    $powertypeid = $_SESSION['current']['power_type_id'];
    $cbbPowerType = frmuser_createCbbPowerType($powertypeid,'frmuser_cbbPowerType','xajax_frmuser_loadGrid(this.value)');    
    $objResponse->addAssign('frmuser_tdPowertype', 'innerHTML',$cbbPowerType);   
        $power_type_id = $_SESSION['current']['power_type_id'];
    if($power_type_id==POWER_CUSTOMER){
        $objResponse->addScript("$('#frmuser_tdleft').css('display','none');");
        $objResponse->addScript("$('#frmuser_tdmiddle').css('display','none');"); 
    }
    return $objResponse->getXML();   
}
/*
* HÃ m dÃ¹ng Ä‘á»ƒ táº¡o chuá»•i combobox cho loáº¡i quyá»?n.
* @param 1 : $powertypeid - Loáº¡i quyá»?n cá»§a user Ä‘Äƒng nháº­p
* @param 2 : tÃªn hÃ m Ä‘ 
*/
function frmuser_createCbbPowerType($powertypeid,$id,$onChangeFunc='',$all=false){
    global $objDbSelect,$lang;
    $condition = '';
    switch($powertypeid){
       case POWER_MASTER : $condition = ' and (id = '.POWER_SUPERVISOR. ' or id = ' .POWER_ACCOUNT. ' or id = ' .POWER_AGENCY. ' or id = ' .POWER_SUPPORTER.')'; break;
       case POWER_SUPERVISOR : $condition = ' and (id = ' .POWER_ACCOUNT. ' or id = ' .POWER_AGENCY.' or id = ' .POWER_SUPPORTER.')'; break;
       case POWER_AGENCY : $condition = 'and (id = '.POWER_SUB_CUSTOMER. ' or id = '.POWER_CUSTOMER. ' or id = ' .POWER_CUSTOMER_DEMO. ' or id = ' .POWER_MANAGER_CUSTOMER.')'; break;
       default : $condition = ' and 1 = 0'; break;
    }
    $sql =  "select id as id, name_$lang as name from tbl_power_type where active = 1 $condition ";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $strResult .= "<option value ='$value'>$name</option>";
    }
    $addFunction ='';    
    if($onChangeFunc!=''){
        $addFunction = "onchange='$onChangeFunc'";
    }
    $firstOption = '';
    if($all){
      $firstOption = "<option value ='' > --Tất cả-- </option> "; 
    }else{
      $firstOption = "<option value ='-1' > --Chọn-- </option> ";   
    }
    $strResult ="<select id='$id' class='cbbRequired' $addFunction>$firstOption".$strResult.'</select>';  
    return   $strResult;
}

/*
* HÃ m dÃ¹ng Ä‘á»ƒ táº¡o chuá»•i combobox cho loáº¡i quyá»?n.
* @param 1 : $powertypeid - Loáº¡i quyá»?n duoc truyen vao
* @param 2 : tÃªn hÃ m Ä‘ 
*/
function frmuser_createCbbPower($powertypeid,$id,$onChangeFunc='',$all=false){
    global $objDbSelect,$lang;
    $condition = '';
    $sql =  "select id as id, name_$lang as name from tbl_power where active = 1 and power_type_id = '$powertypeid'  ";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $strResult .= "<option value ='$value'>$name</option>";
    }
    $addFunction ='';    
    if($onChangeFunc!=''){
        $addFunction = "onchange='$onChangeFunc'";
    }
    $firstOption = '';
    if($all){
      $firstOption = "<option value ='' > --Tất cả-- </option> "; 
    }else{
      $firstOption = "<option value ='-1' > --Chọn-- </option> ";   
    }
    $strResult ="<select id='$id' $addFunction>$firstOption".$strResult.'</select>';  
    return   $strResult;
}
 
function frmuser_save($arrParams){

    global $objDbSelect;
    $objResponse = new xajaxResponse();    
    $username       =     addslashes(strtolower($arrParams['txtAddUsername']));
   // $pass           =     EncryptPass($arrParams['txtAddUsername']);
    $pass           =     EncryptPass(PASS_DEFAULT);
    $power_type_id  =     $arrParams['cbbPowerType'];
    $edit           =     $arrParams['hdEdit'];
    $id             =     $arrParams['hdId'];
    $email          =     addslashes($arrParams['txtEmail']);
    $address        =     addslashes($arrParams['txtAddress']);
    $phone          =     addslashes($arrParams['txtPhone']);
    $userid = $_SESSION['current']['user_id'];

    $sql = '';
    if($edit==1){
           $sql = "UPDATE tbl_user SET username='$username',power_type_id='$power_type_id',email='$email',address='$address',phone=$phone,user_created='$userid' WHERE id = '$id'";
    }else{
           $sql = "insert into tbl_user(username,pass,power_type_id,email,address,phone,active,user_created)
    value('$username','$pass','$power_type_id','$email','$address','$phone',0,'$userid')";
    }    

    if(!$objDbSelect->Execute($sql)){
        $objResponse->addAlert('LÆ°u tháº¥t báº¡i');
    }
    $objResponse->addScript('xajax_frmuser_ajxLoadDataUser();');
    $objResponse->addScript('xajax_frmuser_loadGrid();');
    return $objResponse->getXML(); 
 }
 
function frmuser_ajxLoadDataUser($arrParams=array()){     
    global $lang,$objDbSelect;
    $power_type = isset($_SESSION['current']['power_type'])?$_SESSION['current']['power_type']:4;
    $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
    $arrUser = frmuser_GetUserGrid($arrParams);
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $tmplGrid = 'templates/'.$lang."/main_users_grid.html"; 
    if(!file_exists($tmplGrid)){
      $objResponse->addAlert('KhÃ´ng cÃ³ quyá»?n truy cáº­p chá»©c nÄƒng nÃ y');
      $objResponse->addRedirect('index.php?m=home');  
      return $objResponse->getXML();
    }
    $oTbs->LoadTemplate($tmplGrid);
    $oTbs->MergeBlock('blk_data', $arrUser);
    $oTbs->Show(TBS_NOTHING);    
    $objResponse->addAssign('divGrid', 'innerHTML', $oTbs->Source);   
    $objResponse->addScriptCall('initTable();'); 
    $objResponse->addScript('changeWaiting(1);');                             
    return $objResponse->getXML();
}

function frmuser_GetUserGrid($arrParams= array()){
    global $objDbSelect;
    $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
    $condition = '';
    $sql= "select u.id,u.active,username,pt.name_$lang as power_type,p.power_name_$lang as power ,u.address,u.phone,u.email from 
    tbl_user u join tbl_power_type as pt on pt.power_type_id = u.power_type_id join tbl_power  as p on  p.id = u.power_id  ";
    $arr = $objDbSelect->GetArray($sql);
    return $arr;
}

function frmuser_loadGrid($power_id=''){
    global $objDbSelect,$lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $lang ='vi';
    $arrGrid = frmuser_getGrid($power_id);
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/main_user_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmuser_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("dataTableObj.CreatDataTableGroup('frmuser_tbl');"); 
    $objResponse->addScript('changeWaiting(1);');                             
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function frmuser_getGrid($power_id=''){
    global $objDbSelect,$lang;
    $lang = 'vi';
    $condition = '';
    $powertypeid = $_SESSION['current']['power_type_id'];
    $userid = $_SESSION['current']['user_id'];
    $condByPower = '';
    if($power_id!=''&&$power_id!='-1'){
        $condByPower .=' AND u.power_type_id = "'.$power_id.'"';
    }
    switch($powertypeid){
       case POWER_MASTER : $condition = ' and (u.power_type_id = '.POWER_SUPERVISOR. ' or u.power_type_id = ' .POWER_ACCOUNT. ' or u.power_type_id = ' .POWER_AGENCY. 
       ' or u.power_type_id = ' .POWER_SUPPORTER.')'; break;
       case POWER_SUPERVISOR : $condition = ' and (u.power_type_id = ' .POWER_ACCOUNT. ' or u.power_type_id = ' .POWER_AGENCY.' or u.power_type_id = ' .POWER_SUPPORTER.')'; break;
       case POWER_AGENCY : $condition = 'and (u.power_type_id = '.POWER_CUSTOMER. ' or u.power_type_id = ' .POWER_CUSTOMER_DEMO. ' or u.power_type_id = ' .POWER_MANAGER_CUSTOMER.') and u.user_created = '."'$userid'"; break;
        case POWER_CUSTOMER :
        $hotel_id = $_SESSION['current']['hotel_id'];
         $condition = 'and (u.power_type_id = ' .POWER_SUB_CUSTOMER.') and u.hotel_id = '."'$hotel_id'"; break;
       default : $condition = ' and 1 = 0'; break;
    }
    $sqlSelect = "select u.*,pt.name_$lang as powertype from tbl_user u join tbl_power_type pt on u.power_type_id = pt.id where 1 = 1 $condition $condByPower  order by name_vi";
   // echo $sqlSelect; die();
    return $objDbSelect->GetArray($sqlSelect);
}

function frmuser_changePass($newpass,$id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $pass        =   EncryptPass($newpass);
    $sql         =  "UPDATE tbl_user SET pass ='$pass' WHERE id =$id";
    if(!$objDbSelect->Execute($sql)){
       $objResponse->addAlert('Ä?á»•i máº­t kháº©u tháº¥t báº¡i');
    }
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax 
}

function frmuser_edit($id){
    global $objDbSelect,$lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $sql         = "select * from tbl_user where id = $id";
    $arr         = $objDbSelect->GetArray($sql);
    
    $username = $arr[0]['username'];
    $email = $arr[0]['email'];
    $phone = $arr[0]['phone'];
    $power_type_id = $arr[0]['power_type_id'];
    $address = $arr[0]['address'];
    
    $objResponse->addScript("$('#frmuser_txtAddUsername').val('$username')");
    $objResponse->addScript("$('#frmuser_txtEmail').val('$email')");
    $objResponse->addScript("$('#frmuser_txtPhone').val('$phone')");
    $objResponse->addScript("$('#frmuser_cbbPowerType').val('$power_type_id')");
    $objResponse->addScript("$('#frmuser_txtAddress').val('$address')"); 
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax 
} 
?>
